require.config({
  baseUrl: "./js/modules-cart",
  paths: {
    jq: "../libs/jquery.2.2.4",
    getJson: "../public/getJson",
    load: "../public/load",
    render: "render",
    setLocal: "setLocal",
    allNum: "allNum",
    allPrice: "allPrice",
    allFlag: "allFlag",
    allWeight: "allWeight",
  },
});
require(["jq","getJson","render","setLocal","allNum","allPrice","allWeight","allFlag","load"], function (_, gj, r, sl, allNum, allPrice, allWeight, allFlag,ld) {
  ld(document.cookie.split("=")[2]);
  let localData = localStorage.getItem("goodsMsg")? JSON.parse(localStorage.getItem("goodsMsg")): [];
  $(".allNum").html(allNum(localData));
  $(".allPrice").html("￥" + allPrice(localData).toFixed(2));
  $(".allWeg").html(allWeight(localData).toFixed(2) + "kg");
  $("#allFlag")[0].checked = allFlag(localData);

  gj({
    url: "../json/goods-list.json",
  }).then((res) => {
    res = JSON.parse(res);
    r(res, localData, $("tbody"));
  });
//   更改数量
  $("tbody").on("input", ".setNum", function () {
    let id = $(this).parent().parent().attr("index");
    sl(localData, id, (i) => {
      localData[i].num = $(this).val();
      $(this).parent().next().next().next().html("￥"+(localData[i].num*localData[i].price).toFixed(2))
    });
    $(".allNum").html(allNum(localData));
    $(".allPrice").html("￥" + allPrice(localData).toFixed(2));
    $(".allWeg").html(allWeight(localData).toFixed(2) + "kg");
  });
//   删除
  $("tbody").on("click", ".del", function () {
    let id = $(this).parent().attr("index");
    $(this).parent().remove();
    sl(localData, id, (i) => {
      localData.splice(i, 1);
    });
    $(".allPrice").html("￥" + allPrice(localData).toFixed(2));
    $(".allWeg").html(allWeight(localData).toFixed(2) + "kg");
  });
//   选择
  $("tbody").on("input", ".check", function () {
    let id = $(this).parent().parent().attr("index");
    sl(localData, id, (i) => {
      localData[i].flag = Number(this.checked);
    });

    $(".allPrice").html("￥" + allPrice(localData).toFixed(2));
    $(".allWeg").html(allWeight(localData).toFixed(2) + "kg");
    $("#allFlag")[0].checked = allFlag(localData);
  });
// 全选
  $("#allFlag").on("input", function () {
    localData.forEach((val) => {
      val.flag = Number(this.checked);
    });
    localStorage.setItem("goodsMsg", JSON.stringify(localData));

    for (let i = 0; i < $(".check").length; i++) {
      $(".check")[i].checked = this.checked;
    }
    $(".allPrice").html("￥" + allPrice(localData).toFixed(2));
    $(".allWeg").html(allWeight(localData).toFixed(2) + "kg");
  });
});
